/*
 * 用户注册界面
 */
package com.wonyesoft.wuliu.acticity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.User;
import com.wonyesoft.wuliu.overriding.CornerListView;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;

public class UserCenterActivity extends BaseActivity implements OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right,user_info_name_text,user_info_phone_text;
	private ImageView head_left;
	private Gson gson = new Gson();
	private HttpUtils http = new HttpUtils();
	private RequestParams params;
	private AppContent app;
	private PreferencesCookieStore preferencesCookieStore;
	private RelativeLayout user_info,user_yue,user_jifen,user_dingdan,user_dizhi,user_newpsw;
	// 小雨list
	private CornerListView listview;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> list;
	private int[] img = { R.drawable.user_myinfo, R.drawable.user_mydingdan,
			R.drawable.user_changyong, R.drawable.user_newpsw };
	//用户中心
	private RelativeLayout userinfo;
	private ArrayList<User> aUsers;
	private User user;
	private Button user_zhuxiao;
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user);
		System.out.println("regsiter");
		title = (TextView) findViewById(R.id.head_title);
		user_info_name_text = (TextView) findViewById(R.id.user_info_name_text);
		user_info_phone_text = (TextView) findViewById(R.id.user_info_phone_text);
		head_left = (ImageView) findViewById(R.id.head_left);
		head_right = (TextView) findViewById(R.id.head_right);
		//head_left.setVisibility(0);
		title.setText("用户中心");
		//showLoadingDialog("用户信息载入中...");
		getUserInfo();
//		xiaoyuMyList();
		
		
		
		// 小雨哥注释的
//		user_info = (Button) findViewById(R.id.user_info);
//		user_address = (Button) findViewById(R.id.user_address);
//		user_mylist = (Button) findViewById(R.id.user_my);
//		user_newpassword = (Button) findViewById(R.id.user_newpassword);
		user_zhuxiao = (Button) findViewById(R.id.user_zhuxiao);
		head_left.setOnClickListener(this);
//		user_info.setOnClickListener(this);
//		user_address.setOnClickListener(this);
//		user_mylist.setOnClickListener(this);
//		user_newpassword.setOnClickListener(this);
		user_zhuxiao.setOnClickListener(this);
		user_info = (RelativeLayout) findViewById(R.id.user_info_layout);
		user_yue = (RelativeLayout) findViewById(R.id.user_yue_layout);
		user_jifen = (RelativeLayout) findViewById(R.id.user_jifen_layout);
		user_dingdan = (RelativeLayout) findViewById(R.id.user_dingdan_layout);
		user_dizhi = (RelativeLayout) findViewById(R.id.user_dizhi_layout);
		user_newpsw = (RelativeLayout) findViewById(R.id.user_newpsw_layout);
		
		user_info.setOnClickListener(this);
		user_yue.setOnClickListener(this);
		user_jifen.setOnClickListener(this);
		user_dingdan.setOnClickListener(this);
		user_dizhi.setOnClickListener(this);
		user_newpsw.setOnClickListener(this);
		
		

	}

	private void getUserInfo() {
		showLoadingDialog("数据载入中...");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(AppContent.getPreferencesCookieStore());
		http.send(HttpRequest.HttpMethod.POST, GagApi.USERINFO, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						dismissLoadingDialog();
						System.out.println("进入用户基本信息");
						System.out.println("list:" + responseInfo.result);
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						State state = jrs.getState();

						if (state.getCode().equals(State.SUCEECCODE)) {

							// 存用户登录信息
							System.out.println("进来了");
							System.out.println(jrs.getParam("rows") + "");
							Type type = new TypeToken<ArrayList<User>>() {
							}.getType();
							try {
								
								user = gson2.fromJson(
										new JSONObject(responseInfo.result)
												.getJSONObject("data")
												.getJSONArray("rows").getJSONObject(0)
												+ "", User.class);
												
								if(null!=user) 	init();
							} catch (JsonSyntaxException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							System.out.println(jrs.getParam("rows"));

						} else {

							System.out.println(state.getMsg());
							if (state.getCode().equals(State.UNLOGIN)) {
								openActivity(UserLoginActivity.class);
								finish();

							}
						}
						// ToastUtils.showLong(state.getMsg());}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() + ":" +
						// msg);
						dismissLoadingDialog();
						System.out.println(msg);
					}
				});
		
	}

	protected void init() {
		
		user_info_phone_text.setText(user.getMobile());
		user_info_name_text.setText(user.getRealName());
	}

	private List<Map<String, Object>> getData() {
		String[] iname = { "我的资料", "我的订单", "定位查单", "密码修改" };

		for (int i = 0; i < img.length; i++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("img",img[i]);
			map.put("text",iname[i]);
			list.add(map);
		}
		return list;

	}
	
	public void onClick(View v) {   
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		case R.id.user_jifen_layout:
		case R.id.user_yue_layout:
			 showLongToast("该功能暂未对外开放！");
			 break;
  	     case R.id.user_info_layout:   
			openActivity(UserInfoActivity.class);
			break;
		case R.id.user_dizhi_layout:
			openActivity(UserSearchListActivity.class);
			break;
		case R.id.user_dingdan_layout:
			openActivity(OrderListsActivity.class);
			break;
		case R.id.user_newpsw_layout:
			openActivity(UserChangePwdActivity.class);
			break;
		case R.id.user_zhuxiao:
			finish();
			break;
		default:
			break;
		}
	}

}
